package org.magneton.foundation;

import javax.annotation.Nullable;

/**
 * @author zhangmsh 2022/5/18
 * @since 1.0.0
 */
public class MoreClasses {

	private MoreClasses() {
	}

	/**
	 * Return the default ClassLoader to use: typically the thread context ClassLoader, if
	 * available; the ClassLoader that loaded the ClassUtils class will be used as
	 * fallback.
	 * <p>
	 * Call this method if you intend to use the thread context ClassLoader in a scenario
	 * where you clearly prefer a non-null ClassLoader reference: for example, for class
	 * path resource loading (but not necessarily for {@code Class.forName}, which accepts
	 * a {@code null} ClassLoader reference as well).
	 * @return the default ClassLoader (only {@code null} if even the system ClassLoader
	 * isn't accessible)
	 * @see Thread#getContextClassLoader()
	 * @see ClassLoader#getSystemClassLoader()
	 */
	@Nullable
	public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		}
		catch (Throwable ex) {
			// Cannot access thread context ClassLoader - falling back...
		}
		if (cl == null) {
			// No thread context class loader -> use class loader of this class.
			cl = MoreClasses.class.getClassLoader();
			if (cl == null) {
				// getClassLoader() returning null indicates the bootstrap ClassLoader
				try {
					cl = ClassLoader.getSystemClassLoader();
				}
				catch (Throwable ex) {
					// Cannot access system ClassLoader - oh well, maybe the caller can
					// live with null...
				}
			}
		}
		return cl;
	}

}
